package de.seminar.lego3D.views;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;

import de.seminar.lego3D.Constants;
import de.seminar.lego3D.R;
import de.seminar.lego3D.colorPicker.ColorPanelView;
import de.seminar.lego3D.colorPicker.ColorPickerView;
import android.R.color;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.Toast;

public class ColorView extends LinearLayout implements View.OnClickListener {

	/** the colors in a gridview **/
	private GridView colorGrid;

	/** the colors **/
	private ArrayList<Integer> colors = new ArrayList<Integer>();

	/** context of this view **/
	private Context ctx;

	/** handler to send a message to the main activity with the chosen color **/
	private Handler mHandler;

	/** adapter for the grid view **/
	private ColorAdapter adapter;
	
	/** the color picker view **/
	private ColorPickerView colorPickerView;

	/** new color panel **/
	private ColorPanelView newColorPanel;
	
	/** button to add a color to the list **/
	private Button addColorBtn;

	/** the current chosen color **/
	int currentColor;

	/**
	 * Creates a new color with of the given parameters.
	 * @param context - context of the main activity
	 * @param handler - handler to send a message to the main activity with the chosen color
	 * @param objects - array filled with the colors
	 */
	public ColorView(Context context, Handler handler, Object[] objects) {
		super(context);

		ctx = context;
		mHandler = handler;

		for (Object o: objects) {
			colors.add((Integer) o);
		}

		final View content = LayoutInflater.from(context).inflate(
				R.layout.color_screen, null, false);
		
		colorPickerView = (ColorPickerView) content.findViewById(R.id.color_picker_view);
		newColorPanel = (ColorPanelView) content.findViewById(R.id.new_color_panel);
		addColorBtn = (Button) content.findViewById(R.id.add_color);
		
		colorPickerView
		.setOnColorChangedListener(new ColorPickerView.OnColorChangedListener() {

			@Override
			public void onColorChanged(int color) {
				newColorPanel.setColor(color);
				currentColor = color;
			}
		});
		
		colorGrid = (GridView) content.findViewById(R.id.colorGrid);
		
		colorGrid.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> parent, View v, int postiion,
					long id) {

//				Toast.makeText(ctx, "clicked on color", Toast.LENGTH_SHORT)
//						.show();

				final Bundle bundle = new Bundle();
				bundle.putInt(Constants.LEGO_COLOR, colors.get(postiion));
				final Message msg = new Message();
				msg.setData(bundle);
				mHandler.sendMessage(msg);
			}
		});
		
		colorPickerView.setColor(0xff000000, true);
		colorPickerView.setAlphaSliderVisible(true);
		colorPickerView.setSliderTrackerColor(0xffCECECE);
		colorPickerView.setBorderColor(0xff7E7E7E);
		newColorPanel.setBorderColor(colorPickerView.getBorderColor());

		addColorBtn.setOnClickListener(this);

		adapter = new ColorAdapter(ctx);
		colorGrid.setAdapter(adapter);
		
		this.addView(content);
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.add_color:
			if (!colors.contains(currentColor)) {
				colors.add(currentColor);
				adapter.notifyDataSetChanged();
				colorGrid.invalidateViews();
			}
			break;
		}
	}
	
	/**
	 * Clears the color view, and the array.
	 */
	public void clearColors() {
		colors.clear();
		adapter.notifyDataSetChanged();
		colorGrid.invalidateViews();
	}

	public void colorChanged(int color) {
	}
	
	/**
	 * Returns the array filled with the colors
	 * @return
	 */
	public ArrayList<Integer> getColors() {
		return colors;
	}
	
	/**
	 * Sets the color array to the given one and updates the view.
	 */
	public void setColors(Object[] objects) {
		colors.clear();
		for (Object o: objects) {
			colors.add((Integer) o);
		}
		adapter.notifyDataSetChanged();
		colorGrid.invalidateViews();
	}

	/**
	 * Adapter class to fill the gridview.
	 * @author christian
	 *
	 */
	public class ColorAdapter extends BaseAdapter {

		/**
		 * context of the activity
		 */
		private Context context;
		
		/**
		 * the default icon of the color drop
		 */
		private Drawable drawableIcon;

		/**
		 * Creates a new adapter.
		 * @param c
		 */
		public ColorAdapter(Context c) {
			context = c;
			drawableIcon = ctx.getResources().getDrawable(R.drawable.color_drop);
		}

		@Override
		public void notifyDataSetChanged() {
			super.notifyDataSetChanged();
		}

		/**
		 * Returns the number of items in the gridView.
		 */
		public int getCount() {
			return colors.size();
		}

		/**
		 * Returns an object reference to a specific item.
		 */
		public Object getItem(int position) {
			return colors.get(position);
		}

		/**
		 * Returns the id of a item at a position
		 */
		public long getItemId(int position) {
			return position;
		}

		/**
		 *  create a new ImageView for each item referenced by the Adapter
		 */
		public View getView(int position, View convertView, ViewGroup parent) {
//			ImageButton v = new ImageButton(context);
			View v = LayoutInflater.from(ctx).inflate(R.layout.color_image, null, false);
			ImageView img = (ImageView) v.findViewById(R.id.color_view);
			img.setImageResource(R.drawable.color_drop);
			img.setBackgroundColor(colors.get(position));
		//	v.setBackgroundResource(R.drawable.color_drop);
		//	v.setImageDrawable(drawableIcon);
		//	v.setBackgroundColor(colors.get(position));
			return v;
		}
	}

}
